package com.tom.architect02.zframework.wms.skin.higher_lib;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.view.ViewCompat;

import com.tom.architect02.zframework.wms.skin.higher_lib.utils.SkinManager;

import java.util.List;

public class SkinView {

    //多组属性
    private List<ViewAttr> viewAttrs;

    private View view;

    public SkinView(List<ViewAttr> viewAttrs, View view) {
        this.viewAttrs = viewAttrs;
        this.view = view;
    }

    public List<ViewAttr> getSkinItems() {
        return viewAttrs;
    }

    public void setSkinItems(List<ViewAttr> viewAttrs) {
        this.viewAttrs = viewAttrs;
    }

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public void apply() {
        for (ViewAttr viewAttr : viewAttrs) {
            Drawable left = null, top = null, right = null, bottom = null;
            switch (viewAttr.attributeName) {
                case "background":
                    Object background = SkinManager.getInstance().getBackground(viewAttr
                            .resId);
                    //背景可能是 @color 也可能是 @drawable
                    if (background instanceof Integer) {
                        view.setBackgroundColor((int) background);
                    } else {
                        ViewCompat.setBackground(view, (Drawable) background);
                    }
                    break;
                case "src":
                    background = SkinManager.getInstance().getBackground(viewAttr
                            .resId);
                    if (background instanceof Integer) {
                        ((ImageView) view).setImageDrawable(new ColorDrawable((Integer)
                                background));
                    } else {
                        ((ImageView) view).setImageDrawable((Drawable) background);
                    }
                    break;
                case "textColor":
                    ((TextView) view).setTextColor(SkinManager.getInstance().getColorStateList
                            (viewAttr.resId));
                    break;
                case "drawableLeft":
                    left = SkinManager.getInstance().getDrawable(viewAttr.resId);
                    break;
                case "drawableTop":
                    top = SkinManager.getInstance().getDrawable(viewAttr.resId);
                    break;
                case "drawableRight":
                    right = SkinManager.getInstance().getDrawable(viewAttr.resId);
                    break;
                case "drawableBottom":
                    bottom = SkinManager.getInstance().getDrawable(viewAttr.resId);
                    break;
                default:
                    break;
            }
            if (null != left || null != right || null != top || null != bottom) {
                ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(left, top, right,
                        bottom);
            }
        }
    }
}
